package com.situ.mall.controller.back;

import java.io.File;
import java.util.UUID;

import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.situ.mall.common.ServerResponse;

@RequestMapping("/upload")
@Controller
public class UploadController {

	@RequestMapping("/uploadImg")
	@ResponseBody
	public ServerResponse uploadImg(MultipartFile file) {
		String name = UUID.randomUUID().toString().replace("-", "");
		// jpg、png
		String extension = FilenameUtils.getExtension(file.getOriginalFilename());
		// 拼接图片名字
		String filename = name + "." + extension;
		// 图片保存位置
		String filepath = "E:\\img\\" + filename;
		try {
			file.transferTo(new File(filepath));
		} catch (Exception e) {

		}
		return ServerResponse.createSuccess("上传成功", filename);
	}
}
